package com.tsystems.clientEmployee.gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.toedter.calendar.JDateChooser;
import com.tsystems.entities.Employee;


/**
 * @author vlad
 *
 */
public class RegisterPanel extends JPanel {
	
	private static final long serialVersionUID = 5706735981492285638L;
	private JPasswordField password;
	private JTextField loginTxt;	
	private JDateChooser birthdayJDC;
	private JTextField nameTxt;
	private JTextField surnameTxt;
	
	private Employee  result;
	private JPasswordField confirm;

	public RegisterPanel() {
		super(new GridBagLayout());
		setBorder(new EmptyBorder(10, 10, 5, 10));
				
		add(new JLabel("Имя пользователя"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		loginTxt = new JTextField();
		loginTxt.setColumns(15);
		add(loginTxt, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		add(new RequiredFieldLabel(), new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		
		
		add(new JLabel("Пароль"), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		password = new JPasswordField();
		add(password, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		add(new RequiredFieldLabel(), new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		
		
		add(new JLabel("Подтверждение пароля"), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));		
		confirm = new JPasswordField();
		add(confirm, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		add(new RequiredFieldLabel(), new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		
		add(new JLabel("Имя"), new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		nameTxt = new JTextField();
		add(nameTxt, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		
		add(new JLabel("Фамилия"), new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		surnameTxt = new JTextField();
		add(surnameTxt, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		
		
		add(new JLabel("Дата рождения"), new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));	
		birthdayJDC = new JDateChooser(new Date());
		add(birthdayJDC, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		
			
		JButton okBtn = new JButton("Зарегистрироваться");
		JButton cancelBtn = new JButton("Выход");
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(okBtn);
		buttonPanel.add(cancelBtn);
		
		add(buttonPanel, new GridBagConstraints(0, 7, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		
		okBtn.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				Employee employee = new Employee(nameTxt.getText(), surnameTxt.getText(),loginTxt.getText(), new String(password.getPassword()));
				result = employee;
				
				
				if (validate(result)) {
					RegisterPanel.this.getTopLevelAncestor().setVisible(false);		
				}
			}
		});
		
		cancelBtn.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				result = null;
				RegisterPanel.this.getTopLevelAncestor().setVisible(false);				
			}
		});
	}
	
	private boolean validate(Employee employee) {
		String confirmString = new String(confirm.getPassword());
		
		if (employee.getLogin().isEmpty() || employee.getPassword().isEmpty() || confirmString.isEmpty()) {
			JOptionPane.showMessageDialog(RegisterPanel.this, "Заполните поля обязательные для заполнения!");
			return false;
		}
		
		if (!employee.getPassword().equals(confirmString)) {
			JOptionPane.showMessageDialog(RegisterPanel.this, "Пароль и его подтверждение не совпадают.");
			return false;
		}
		
		return true;
	}
	
	private Employee getResult() {
		return result;
	}
	
	class RequiredFieldLabel extends JLabel {
		private static final long serialVersionUID = -419805710242136347L;

		public RequiredFieldLabel() {
			super("*");
			setForeground(Color.RED);
		}
	}
	
	public static Employee showDialog(Window owner) {
		JDialog dialog = new JDialog(owner, "Регистрация");
		dialog.setModal(true);
		RegisterPanel panel = new RegisterPanel();
		
		dialog.setContentPane(panel);
		dialog.pack();
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);	
		
		return panel.getResult();
	}
}
